Imports System.Data.SqlClient
Public Class ListBoxDemo

    Private Sub ListBoxDemo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim folder As String() = System.IO.Directory.GetDirectories("C:\Windows")
        ListBox1.DataSource = folder

        Dim Item As KeyItem
        For i As Integer = 1 To 12
            Item = New KeyItem("Month " & i, i)
            ListBox2.Items.Add(Item)
        Next
        ListBox2.DisplayMember = "Name"
        ListBox2.ValueMember = "Value"


        Dim week(6) As Object


        For i As Integer = 0 To 6
            Item = New KeyItem("Weekday " & (i + 1), i + 1)
            week(i) = Item
        Next
        ListBox3.Items.AddRange(week)
        ListBox3.DisplayMember = "Name"
        ListBox3.ValueMember = "Value"

        'Database
        'Database
        Dim conn As New SqlConnection()
        conn.ConnectionString = " user id=sa;pwd=;data source=.;initial catalog=Northwind"
        Dim da As New SqlDataAdapter("SELECT ProductID, ProductName FROM Products", conn)
        Dim table As New DataTable
        da.Fill(table)

        ListBox4.DataSource = table
        ListBox4.DisplayMember = "ProductName"
        ListBox4.ValueMember = "ProductID"


    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        MsgBox(ListBox1.SelectedValue)
    End Sub



    Private Sub ListBox2_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedValueChanged
        If Not ListBox2.SelectedItem Is Nothing Then
            Dim Item As KeyItem = ListBox2.SelectedItem
            MsgBox("Value=" + Item.Value + ",Name=" + Item.Name)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Not ListBox3.SelectedItems Is Nothing Then
            Dim strValues As String = ""
            For Each Item As KeyItem In ListBox3.SelectedItems
                strValues += Item.Value + ","
            Next
            MsgBox("Days: " + strValues)
        End If

    End Sub

    Private Sub ListBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox4.SelectedIndexChanged
        MsgBox(String.Format("Bạn chọn {0} –Mã số {1}", ListBox4.Text, ListBox4.SelectedValue))

    End Sub
End Class

Class KeyItem
    Private _strValue As String = ""
    Private _strName As String = ""

    Sub New(ByVal sName As String, ByVal sValue As String)
        _strName = sName
        _strValue = sValue
    End Sub
    Public Property Name() As String
        Get
            Return _StrName
        End Get
        Set(ByVal Value As String)
            _StrName = value
        End Set
    End Property
    ' Khai baùo thuoäc tính Value
    Public Property Value() As String
        Get
            Return _StrValue
        End Get
        Set(ByVal Value As String)
            _StrValue = value
        End Set
    End Property

End Class
